package com.gomoney.auth.security.util;

import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;

import java.util.LinkedHashMap;
import java.util.Map;


public class SysUserJwtAccessTokenConverter extends JwtAccessTokenConverter {
    @Override
    public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
        Map<String, Object> additionalInformation = new LinkedHashMap<>();
        Map<String, Object> info = new LinkedHashMap<>();
        /*info.put("author", "chenx");
        info.put("email", "chenxiang_1984@163.com");
        info.put("GitHub", "https://gitee.com/yunke_network_jingzhou/gomoney-oauth2.git
https://gitee.com/yunke_network_jingzhou/gomoney-oauth2-front.git");*/
        //追加token回执参数
        String currentPrincipalName = authentication.getName();
        info.put("name", currentPrincipalName);
        info.put("user", SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString());
        additionalInformation.put("info", info);
        ((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(additionalInformation);
        return super.enhance(accessToken, authentication);
    }
}
